﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Billing.Windows
{
    /// <summary>
    /// Interaction logic for Discount.xaml
    /// </summary>
    public partial class Discount : Window
    {
        BillingGrid m_BillingGrid;
        bool m_discountInRs = true;
        decimal m_TotalAmount = 0;

        public Discount(BillingGrid billingGrid, bool discountInRs)
        {
            try
            {
                InitializeComponent();

                m_BillingGrid = billingGrid;

                txtDiscount.Focus();
                txtDiscount.SelectAll();
                m_discountInRs = discountInRs;
                m_TotalAmount = m_BillingGrid.TotalAmount;
                //lblTotalAfterDiscount.Content = m_BillingGrid.TotalNetAmount;
                lblTotalAfterDiscount.Content = m_TotalAmount;

                if (discountInRs)
                {
                    rDiscountHeaderLabel.Text = "Rs.";
                    lblDiscount.Content = "Discount in Rs.";
                }
                else
                {
                    rDiscountHeaderLabel.Text = "%";
                }
            }
            catch (Exception) { }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                {
                    if (lblInvalid.Visibility == Visibility.Visible)
                    {
                        if (lblInvalid.Content.ToString() == "Enter Discount Reason or Clear the Discount")
                            return;

                        lblInvalid.Content = "Enter valid Discount or Clear the Discount";
                        txtDiscount.Focus();
                    }
                    else if (txtDiscount.IsFocused)
                    {
                        txtReason.Focus();
                    }
                    else if (txtReason.IsFocused)
                    {
                        if (txtDiscount.Text.Trim() != string.Empty && txtReason.Text.Trim() == string.Empty)
                        {
                            lblInvalid.Content = "Enter Discount Reason";
                            lblInvalid.Visibility = Visibility.Visible;
                            return;
                        }

                        m_BillingGrid.DiscountReason = txtReason.Text;
                        this.Close();
                    }
                }
                else if (e.Key == Key.Enter && txtDiscount.IsFocused && lblInvalid.Visibility == Visibility.Collapsed)
                {
                    txtReason.Focus();
                }
                else if (e.Key == Key.Enter && txtReason.IsFocused && lblInvalid.Visibility == Visibility.Collapsed)
                {
                    if (txtDiscount.Text.Trim() != string.Empty && txtReason.Text.Trim() == string.Empty)
                    {
                        lblInvalid.Content = "Enter Discount Reason";
                        lblInvalid.Visibility = Visibility.Visible;
                        return;
                    }

                    m_BillingGrid.DiscountReason = txtReason.Text;
                    this.Close();
                }
            }
            catch (Exception) { }
        }

        private void txtDiscount_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                lblInvalid.Visibility = Visibility.Collapsed;
                if (txtDiscount.Text.Trim() == string.Empty)
                {
                    m_BillingGrid.DiscountInPercentage = 0;
                    m_BillingGrid.DiscountInRs = 0;
                }
                if (txtDiscount.Text.Trim() != string.Empty && m_BillingGrid != null)
                    if (m_discountInRs)
                    {
                        decimal value = 0;
                        if (Decimal.TryParse(txtDiscount.Text, out value))
                        {
                            m_BillingGrid.DiscountInRs = value;
                            m_BillingGrid.DiscountInPercentage = 0;
                        }
                    }
                    else
                    {
                        decimal value = 0;
                        if (Decimal.TryParse(txtDiscount.Text, out value))
                        {
                            m_BillingGrid.DiscountInPercentage = value;
                            m_BillingGrid.DiscountInRs = 0;
                        }
                    }
                if (m_BillingGrid != null)
                {
                    lblTotalAfterDiscount.Content = m_BillingGrid.CalculateNetTotal().ToString();
                    if (lblTotalAfterDiscount.Content.ToString().Contains("-"))
                    {
                        lblInvalid.Visibility = Visibility.Visible;
                        //m_BillingGrid.DiscountInPercentage = 0;
                        //m_BillingGrid.DiscountInRs = 0;
                        //lblTotalAfterDiscount.Content = m_BillingGrid.CalculateNetTotal().ToString();
                    }
                }
            }
            catch (Exception) { }
        }
    }
}
